package com.kaiyiweitong.tongxing.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.activity.MainActivity;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.bean.LoginResData;
import com.kaiyiweitong.tongxing.constant.HttpConstant;
import com.kaiyiweitong.tongxing.constant.LogicConstant;
import com.kaiyiweitong.tongxing.util.SpUtils;
import com.kaiyiweitong.tongxing.util.CommonUtil;
import com.kaiyiweitong.tongxing.util.HttpUtil;
import com.kaiyiweitong.tongxing.util.MyTextUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/16 0016.
 */

public class LoginActivity extends BaseActivity {
    @Bind(R.id.forget_psw)
    TextView mForgetPsw;
    @Bind(R.id.register)
    TextView mRegister;
    @Bind(R.id.login_tv)
    TextView mLoginTv;
    @Bind(R.id.phone_et)
    EditText mPhoneEt;
    @Bind(R.id.psw_et)
    EditText mPswEt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

    }

    @OnClick({R.id.forget_psw, R.id.register, R.id.login_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.forget_psw:
                startActivity(new Intent(LoginActivity.this, ForgetPswActivity.class));
                break;
            case R.id.register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.login_tv:
                if (MyTextUtils.checkTextContent(mPhoneEt).equals("")) {
                    CommonUtil.toast(this, R.string.phone_not_blank);
                } else if (MyTextUtils.checkTextContent(mPswEt).equals("")) {
                    CommonUtil.toast(this, R.string.psw_not_blank);
                } else {
                    login();
                }
                break;
        }
    }

    private void login() {
        Map<String, String> map = new HashMap<>();
        map.put("username", MyTextUtils.checkTextContent(mPhoneEt));
        map.put("password", MyTextUtils.checkTextContent(mPswEt));
        HttpUtil.get(HttpConstant.USER_LOGIN, map, this, new HttpUtil.MyCallBack() {
            @Override
            public void onResult(String result) {
                switch (HttpUtil.getState(result)) {
                    case "1":
                        CommonUtil.toast(LoginActivity.this, R.string.login_success);
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        LoginResData data = HttpUtil.checkResponse(LoginActivity.this, result, LoginResData.class);

                        SpUtils.saveString(LoginActivity.this,LogicConstant.USER_ID,"4");//测试阶段 默认 id=4
                        SpUtils.saveString(LoginActivity.this, LogicConstant.LOGIN_STATUS, LogicConstant.USER_LOGIN);

                        finish();
                        break;
                    case "0":
                        CommonUtil.toast(LoginActivity.this, R.string.phone_psw_incorrect);
                        break;
                }
            }
        });
    }
}
